package com.sk.weichat.ui.me;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.util.AppUtils;

/**
 * @Author: chenzl
 * @ClassName: MessageNotificationActivity
 * @Description: 消息通知
 * @CreateDate: 2019/9/19 17:13
 */
public class MessageNotificationActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout layout_message_notification;
    private TextView tv_status_tips;
    private TextView tv_status;
    private ImageView img_go_gray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_notification);
        getSupportActionBar().hide();
        initView();
    }

    private void initView() {

        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tvTitle = (TextView) findViewById(R.id.tv_title_center);
        tvTitle.setText("消息通知");
        tv_status = findViewById(R.id.tv_status);
        tv_status_tips = findViewById(R.id.tv_status_tips);
        img_go_gray = findViewById(R.id.img_go_gray);
        layout_message_notification = findViewById(R.id.layout_message_notification);
        layout_message_notification.setOnClickListener(this);
        findViewById(R.id.layout_system_notification).setOnClickListener(this);
        if (AppUtils.isNotificationEnabled(this)){
            layout_message_notification.setEnabled(false);
            tv_status.setVisibility(View.VISIBLE);
            tv_status_tips.setVisibility(View.GONE);
            img_go_gray.setVisibility(View.GONE);
        }else {
            layout_message_notification.setEnabled(true);
            tv_status.setVisibility(View.GONE);
            tv_status_tips.setVisibility(View.VISIBLE);
            img_go_gray.setVisibility(View.VISIBLE);

        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (AppUtils.isNotificationEnabled(this)){
            layout_message_notification.setEnabled(false);
            tv_status.setVisibility(View.VISIBLE);
            tv_status_tips.setVisibility(View.GONE);
            img_go_gray.setVisibility(View.GONE);
        }else {
            layout_message_notification.setEnabled(true);
            tv_status.setVisibility(View.GONE);
            tv_status_tips.setVisibility(View.VISIBLE);
            img_go_gray.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_message_notification:
                if (AppUtils.isNotificationEnabled(this)){
                    return;
                }
                gotoSet();
                break;
            case R.id.layout_system_notification:
                startActivity(new Intent(Settings.ACTION_SOUND_SETTINGS));
                break;
        }
    }

    private void gotoSet() {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26) {
            // android 8.0引导
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
        } else if (Build.VERSION.SDK_INT >= 21) {
            // android 5.0-7.0
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", getPackageName());
            intent.putExtra("app_uid", getApplicationInfo().uid);
        } else {
            // 其他
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", getPackageName(), null));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }


}
